package com.clei.netty.decoder;

import com.clei.utils.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * Byte -> String Decoder
 *
 * @author Y
 * @date 2022-01-13
 */
public class ByteToStringDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        int length = msg.readableBytes();
        byte[] bytes = new byte[length];
        msg.readBytes(bytes);
        String res = ByteUtil.toHex(bytes);
        out.add(res);
    }
}
